<?php
/**
 * Adds pictures to galleries
 */
include ("configs/config.php");


if ( $_SESSION['LOGIN_NAME'] <> "admin" ) 
	{
	header("Location: login-admin.php");
	exit();
	}




$tpl = new XTemplate("./template/tpl_admin.html");

$tpl->parse("main.body.menu");



$query=mysql_query("SELECT * FROM a_gallery order by name");

if (mysql_num_rows($query)>0)
 {
 	while ($temp = mysql_fetch_array($query))
 		{
 		 $group.='<option value="'.$temp['id'].'">'.$temp['name'].'</option>';
 		}

 }
 else
 {
 	$msg_error="There was a error. <BR> Please verify :<BR>";
 	$error++;
 	$msg_error.="There was a problem with the database! Please come back in 10 minutes";	
 	exit();
 }
$error=0;
$tpl->assign("gallery",$group);
if ($_POST["submit"]<>"")
	{

	$title=$_POST["title"];
	$msg_error="There was a error . Please verify :<BR>";
	$date=$_POST["date"];
	
/*	if ($date<>"")
	{
		ereg('^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$',"$date",$rez);
		if ($rez[0]<>"")
		{
			$dt=explode("-",$date);
			$xx=checkdate($dt[1],$dt[2],$dt[0]);
			if (!$xx)
			{
				$msg_error.="-if the date is valid";
				$error++;
				
			}
		}	
		else
		{
			$msg_error.="-if the date is valid";
			$error++;	
		}	
		
	}
	else
	{
		$msg_error.="-if the date is valid";
		$error++;	
	}

	if (check_title($title)<>"1" and $_SESSION["add_what"]<>"link")
	  {
	
	  	$error++;
	  	$msg_error.="- if the title of the picture contains another characters then letters,digits and '-' or '_' <BR>";
	 
	  }
	
*/	
	
	$category=$_POST['gallery'];
   	if ($category[0]=="" and $_SESSION["add_what"]=="picture")
   	{	
   		$error++;
   		$msg_error.="-if you choose a gallery";	
	
	}
	
/*	if ($_POST['picture_url'] <> "" AND  $_SESSION["add_what"]=="picture")
		{
			$tmp_picture_url=check_url ($_POST['picture_url']);
			if ($tmp_picture_url <> 1)
			{
				$error++;
				$msg_error.="<br> - if you enter a valid url for this picture ";
			}
		}
*//*	
	if ($_POST['url']=="" and ($_SESSION["add_what"]=="banner" or $_SESSION["add_what"]=="link"))
	{
		$error++;
   		$msg_error.="-if you enter a valid url";		
	}
	
	if ($_POST["picture_title"]=="" and $_SESSION["add_what"]=="banner")
	{
		$error++;
   		$msg_error.="-if you enter a valid text for the link of the baner";		
	}
*/	
	if ($error==0)
		{
			
			if ($_FILES["picture"]["error"]>0)
			{	
				$error++;
				$msg_error.="-upload error";
			}
			else
			{	
/*				if ( $_SESSION["add_what"] == "link" )
					{
						if (($_POST["picture_title"]<>"") and ($_POST["url"]<>""))
						{
							
							$url="http://".$_POST["url"];
							$query=('insert into a_gallerypics(add_date,picture_title,url,validation) values("'.$date.'","'.$_POST["picture_title"].'","'.$url.'",1);');
							
							
							$url="";
							mysql_query($query);
							header("Location: add-picture.php");
							exit();
						}
						else
						$error++;
						$msg_error.="-that you enter a title and a url ";	
					}
					
				else
				{
*/					
/*						if ($_SESSION["add_what"]=="banner")
							{
//								$title="banner-".$title;//old by razvan
//								$title="banner-".$title;//new by fai

							}
*/						
						//$tmp_picture_url="http://".$_POST['picture_url'];
                        $tmp_picture_url = addHttpString($_POST['picture_url']);
						$res=mysql_query('insert into a_gallerypics (name,add_date,description,picture_title,picture_url,gallery_id) values ("'.$title.'","'.$date.'","'.$_POST["description"].'","'.$_POST["picture_title"].'","'.$tmp_picture_url.'","'.$_POST['gallery'].'");');
						$id = mysql_insert_id();
						$title = makePicFName($_POST['gallery'], $id); 
						$path=$AG_PATH."pictures/$title.jpg";	
						move_uploaded_file($_FILES["picture"]["tmp_name"],$path);
				    	copy($path,$AG_PATH.'/pictures/backup/'.$title.'.jpg');
				    	chmod($path, 0777); 
				    	fai_imageresize_big_picture($AG_PATH."pictures/$title.jpg", $AG_PATH."pictures/thumb-$title.jpg",300,80);
				    	fai_imageresize_big_picture($AG_PATH."pictures/$title.jpg", $AG_PATH."pictures/$title.jpg",600,60);
				    	mysql_query("UPDATE a_gallerypics SET name='$title' WHERE id='$id'");

				    	//$x=exif_imagetype($path);
/*					$x=1;
				    	if ($x>0)
				    		{
				    			
				    			if ($_SESSION["add_what"]=="picture")
				    				{
				    						$tmp_picture_url="http://".$_POST['picture_url'];
				    						$res=mysql_query('insert into a_gallerypics (name,add_date,description,picture_title,picture_url,gallery_id) values ("'.$title.'","'.$date.'","'.$_POST["description"].'","'.$_POST["picture_title"].'","'.$tmp_picture_url.'","'.$_POST['gallery'].'");'); 
					    		  	}
*//*				    		  	else
				    		  		{
					    		  		$url="http://".$_POST["url"];
					    		  		$query='replace into a_gallerypics (name,add_date,picture_title,url) values ("'.$title.'","'.$date.'","'.$_POST["picture_title"].'","'.$url.'");';
					    		  		$res=mysql_query($query); 
					    		  		//echo $query;
				    		  		}
*/				    		  		
/*				    		   	if (mysql_affected_rows()<1)
				    		   	 {
				    		   	 	$error++;
				    		   		$msg_error.="- file named $title already exist</br>"; 
				    		   	}
*//*				    		   	else
				    		   	{
				    		   		if ($_SESSION["add_what"]=="picture")
					    		   	{	
					    		   		$category=$_POST['gallery'];
					    		    	$last_picture_id=mysql_insert_id($link);
					    		    	foreach($category as $temp)
					    					{
					    						//!!!TODO
					    						$query="replace into pictures_group (pictures,groups) values ($last_picture_id,$temp);";
					    						$result=mysql_query($query);
					    					}
				    				
				    				}
				    	
				    		    }
*/				    		   
				    		    
				    		
/*				    		}
				    	else
				    	   	{	
				    		$error++;
				    		$msg_error.="-this file is not a jpg file</br>";	
				    		}
*///				    }	
			}	    
	}

	if ( $error > 0 )		
	{
		$tpl->assign("msg_error",$msg_error);
		$tpl->parse("main.body.add_picture_gallery.error");
	}
		else
	{
		    header("Location: add-picture-gallery.php?title=$title"); 
	    exit();
	}		
}
/*
if ($_GET['apply']<>"") 
{
	
	
	$query="update  a_gallerypics set validation=1 where name='$title'";
	
	$res=mysql_query($query);
	
	
	
	
	unlink($PATH."pictures/backup/$title.jpg");
	header("Location: index.php"); 
	exit();

			
}		
  
if ($_GET["cancel"]<>"")
{
	unlink($PATH."pictures/$title.jpg");
	copy( $PATH."pictures/backup/$title.jpg",$PATH."pictures/$title.jpg");	
	
}

if ($_GET["rotater"]<>"")
{
	rotate_picture_left($PATH."pictures/$title.jpg",$PATH."pictures/temp/mod-$title.jpg","right");
	unlink($PATH."pictures/$title.jpg");
	rename( $PATH."pictures/temp/mod-$title.jpg",$PATH."pictures/$title.jpg");	
	fai_imageresize_big_picture($PATH."pictures/$title.jpg", $PATH."pictures/temp/thumb-$title.jpg",300,80);
	if ($_SESSION["add_what"]=="picture")
	{
		unlink($PATH."pictures/thumb-$title.jpg");
		rename( $PATH."pictures/temp/thumb-$title.jpg",$PATH."pictures/thumb-$title.jpg");
	}
}

if ($_GET["rotatel"]<>"")
{
	
	rotate_picture_left($PATH."pictures/$title.jpg",$PATH."pictures/temp/mod-$title.jpg","left");
	unlink($PATH."pictures/$title.jpg");
	rename( $PATH."pictures/temp/mod-$title.jpg",$PATH."pictures/$title.jpg");
	fai_imageresize_big_picture($PATH."pictures/$title.jpg", $PATH."pictures/temp/thumb-$title.jpg",300,80);
	if ($_SESSION["add_what"]=="picture")
	{
		unlink($PATH."pictures/thumb-$title.jpg");
		rename( $PATH."pictures/temp/thumb-$title.jpg",$PATH."pictures/thumb-$title.jpg");
	}
}

if ($_GET["undo"]<>"")
{
	$title=$_GET["title"];
	$query=('delete from pictures where name="'.$title.'";');
	mysql_query($query);
	
	
	unlink($PATH."/pictures/$title.jpg");
	header("Location: index.php");
	exit();
}



if ($_GET['title']<>"")
{
    
  
	

	if ($_GET['modify_clicked']==1 && $_GET['cancel']=="" && $_GET["rotater"]=="" && $_GET['rotatel']=="")
	{
		
		fai_imageresize_big_picture($PATH."pictures/$title.jpg", $PATH."pictures/temp/mod-$title.jpg",$_GET['width'],$_GET['jpeg_quality']);
		
		unlink($PATH."pictures/$title.jpg");
		rename( $PATH."pictures/temp/mod-$title.jpg",$PATH."pictures/$title.jpg");
		
		
		if ($_SESSION["add_what"]=="picture")
		{
			fai_imageresize_big_picture($PATH."pictures/$title.jpg", $PATH."pictures/temp/thumb-$title.jpg",300,80);
			unlink($PATH."pictures/thumb-$title.jpg");
			rename( $PATH."pictures/temp/thumb-$title.jpg",$PATH."pictures/thumb-$title.jpg");
		}
	}	
	
	if ($_GET["width"]<>"")
	{
		$_SESSION["width"]=$_GET["width"];
	}
	else
	{
		if ($_SESSION["width"]=="")
			{
				$_SESSION["width"]=600;
			}
		
	}	
	
	$width=$_SESSION["width"];
	
	if ($_GET["jpeg_quality"]<>"")
	{
		$_SESSION["jpeg_quality"]=$_GET["jpeg_quality"];
	}
	else
	{
		if ($_SESSION["jpeg_quality"]=="")
		{
			$_SESSION["jpeg_quality"]=80;
		}
	}
	
	$jpeg_quality=$_SESSION["jpeg_quality"];
	
	if ($_SESSION["add_what"]=="picture")
		{$tpl->assign("thumb","thumb-$title");
		$tpl->parse("main.body.modify_picture.thumb");	
		}
	
	
	$tpl->assign("width",$width);
	$tpl->assign("jpeg_quality",$jpeg_quality);
	$tpl->assign("title",$title);
	$prop=getimagesize($PATH."pictures/$title.jpg");
	$tpl->assign("title",$title);
	$tpl->assign("picture_width",$prop[0]);
	$tpl->assign("height",$prop[1]);
	
	$size=(filesize($PATH."pictures/$title.jpg"))/1024;
	$size=number_format($size,2);
	$tpl->assign("file_size",$size);
	$tpl->parse("main.body.modify_picture");
	$tpl->parse("main.body");
	$tpl->parse("main");
	$tpl->out("main");
    exit();

}	




$tpl->assign("data2",date("Y-m-d"));
if ($_GET["option"]<>"")
	{$tpl->assign("option",$_GET["option"]);}
else
	{$tpl->assign("option",$_POST["option"]);}	


if ($_GET["option"]=="add_picture" or $_POST["option"]=="add_picture")
{	*/
	$_SESSION["add_what"]="picture";
	$_SESSION["width"]="";
	$tpl->assign("picture_title",$_POST["picture_title"]);
	$tpl->assign("title",$_POST["title"]);
	$tpl->assign("item","Add Picture to gallery");
	$tpl->assign("picture_url",$_POST['picture_url']);
	$tpl->assign("description",$_POST["description"]);
	//$tpl->parse("main.body.add_picture_gallery.picture_title");
	$tpl->parse("main.body.add_picture_gallery.picture_url");
	$tpl->parse("main.body.add_picture_gallery.description");
	$tpl->parse("main.body.add_picture_gallery.group");
	$tpl->parse("main.body.add_picture_gallery.input_file");

	/*
}

if($_GET["option"]=="add_banner" or $_POST["option"]=="add_banner")
{
	$_SESSION["add_what"]="banner";
	$_SESSION["width"]="";
	if ($_POST["picture_title"]<>"")
			{$tpl->assign("picture_title",$_POST["picture_title"]);}
	else
		{$tpl->assign("picture_title","Off Duty? Featured Sites Of The Day");}
			
	$tpl->assign("url",$_POST["url"]);
	$tpl->assign("title",$_POST["title"]);
	$tpl->assign("item","Add Banner");
	$tpl->parse("main.body.add_picture.picture_title");
	$tpl->parse("main.body.add_picture.input_file");
	$tpl->parse("main.body.add_picture.url");

}


if($_GET["option"]=="add_link" or $_POST["option"]=="add_link")
{
	$_SESSION["add_what"]="link";
	if ($_GET["add_ok"]<>"")
	{$tpl->assign("item","Link Added ");}
	else
	{$tpl->assign("item","Add Link ");}
	
	
	$tpl->assign("picture_title",$_POST["picture_title"]);
	$tpl->assign("url",$_POST["url"]);
	
	$tpl->parse("main.body.add_picture.url");
}
*/  
$tpl->parse("main.body.add_picture_gallery");	
$tpl->parse("main.body");
$tpl->parse("main");
$tpl->out("main");
//
function makePicFName($gallery_id, $picid){
	$sql = "SELECT name FROM a_gallery WHERE id='$gallery_id'";
	$query = mysql_query($sql) or die(mysql_error().$sql);
	if($query){
		$row = mysql_fetch_assoc($query);
		if (isset($row['name'])){
			return strtr($row['name']."-$picid",' ','_');
		}
	}
	return '';
}
?>
